package io.sage.modules.basics.controller;

import java.util.*;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.sage.common.validator.ValidatorUtils;
import io.sage.modules.basics.entity.BGoodsUnit;
import io.sage.modules.basics.entity.BLocationPosition;
import io.sage.modules.basics.service.BLocationPositionService;
import io.sage.modules.sys.entity.SysUserEntity;
import io.sage.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.sage.modules.basics.entity.BLocation;
import io.sage.modules.basics.service.BLocationService;
import io.sage.common.utils.PageUtils;
import io.sage.common.utils.R;



/**
 * 位置管理

 *
 * @author zhang
 * @email ${email}
 * @date 2019-08-10 09:12:17
 */
@RestController
@RequestMapping("basics/blocation")
public class BLocationController {
    @Autowired
    private BLocationService bLocationService;
    @Autowired
    private BLocationPositionService bLocationPositionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("basics:blocation:list")
    public R list(@RequestParam Map<String, Object> params){
       // PageUtils page = bLocationService.queryPage(params);

        Integer pageSize = 1;
        if (params != null && params.get("limit") != null) {
            pageSize = java.lang.Integer.parseInt(params.get("limit").toString());
        }
        Integer pageNum = 10;
        if (params != null && params.get("page") != null) {
            pageNum = java.lang.Integer.parseInt(params.get("page").toString());
        }

        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        if(params.get("name")!=null){

            if(!"".equals(params.get("name").toString())){
                paramMap.put("name", params.get("name").toString());
            }
        }
        if(params.get("systemId")!=null){

            if(!"".equals(params.get("systemId").toString())){
                paramMap.put("systemId",Integer.parseInt(params.get("systemId").toString()));
            }
        }
        if(params.get("warehouseId")!=null){

            if(!"".equals(params.get("warehouseId").toString())){
                paramMap.put("warehouseId",Integer.parseInt( params.get("warehouseId").toString()));
            }
        }
        paramMap.put("isDel", 0);
        paramMap.put("orderby", "l.update_time desc,l.create_time desc");
        PageHelper.startPage(pageNum, pageSize);// 设置分页的起始码以及页面大小
        List<BLocation> bLocations = bLocationService.queryByMap(paramMap);

        PageInfo<BLocation> pageInfo = new PageInfo<BLocation>(bLocations);// 传入list就可以了
        return R.ok().put("page", pageInfo);
    }
    /**
     * 列表
     */
    @RequestMapping("/getList")
    public R getList(@RequestParam Map<String, Object> params){
       // PageUtils page = bLocationService.queryPage(params);


        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        if(params.get("name")!=null){

            if(!"".equals(params.get("name").toString())){
                paramMap.put("name", params.get("name").toString());
            }
        }
        if(params.get("systemId")!=null){

            if(!"".equals(params.get("systemId").toString())){
                paramMap.put("systemId",Integer.parseInt(params.get("systemId").toString()));
            }
        }
        if(params.get("warehouseId")!=null){

            if(!"".equals(params.get("warehouseId").toString())){
                paramMap.put("warehouseId",Integer.parseInt( params.get("warehouseId").toString()));
            }
        }
        paramMap.put("isDel", 0);
        paramMap.put("orderby", "l.update_time desc,l.create_time desc");
       // PageHelper.startPage(pageNum, pageSize);// 设置分页的起始码以及页面大小
        List<BLocation> bLocations = bLocationService.queryByMap(paramMap);

        return R.ok().put("list", bLocations);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("basics:blocation:info")
    public R info(@PathVariable("id") Integer id){
        BLocation bLocation = bLocationService.queryById(id);

        return R.ok().put("bLocation", bLocation);
    }
    /**
     * 获取方位下拉框
     */
    @RequestMapping("/getPostionList")

    public R getPostionList(){
      List<BLocationPosition>list=bLocationPositionService.queryAll(new BLocationPosition());

        return R.ok().put("list", list);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("basics:blocation:save")
    public R save(@RequestBody BLocation bLocation){
       SysUserEntity sysUserEntity= ShiroUtils.getUserEntity();
       bLocation.setCreateId(sysUserEntity.getUserId().intValue());
       bLocation.setCreateTime(new Date());
       bLocation.setUpdateId(sysUserEntity.getUserId().intValue());
       bLocation.setUpdateTime(new Date());
        bLocationService.insert(bLocation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("basics:blocation:update")
    public R update(@RequestBody BLocation bLocation){
        ValidatorUtils.validateEntity(bLocation);
        SysUserEntity sysUserEntity= ShiroUtils.getUserEntity();
        bLocation.setUpdateId(sysUserEntity.getUserId().intValue());
        bLocation.setUpdateTime(new Date());
        bLocationService.update(bLocation);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("basics:blocation:delete")
    public R delete(@RequestBody Integer[] ids){
        bLocationService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
